package cn.com.shopec.erp.stock.commo;

import java.util.List;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.product.entity.ProductStock;
import cn.com.shopec.erp.purchase.entity.StockPurchase;
import cn.com.shopec.erp.stock.entity.ProductBarCode;
import cn.com.shopec.erp.stock.entity.StockOutProduct;
import cn.com.shopec.erp.stock.vo.StockInProductVo;

public class StockProductException extends RuntimeException {

	private static final long serialVersionUID = -4826802918428793287L;
	private String prodNo;
	private String exceptionInfo;
	private long stockoutQuantity;

	public StockProductException(StockOutProduct stockOutProduct, long defectiveQuantity, long qualifiedQuantity) {
		this.prodNo = stockOutProduct.getProdNo();
		this.stockoutQuantity = stockOutProduct.getStockoutQuantity();
	}

	public StockProductException(StockOutProduct stockOutProduct, StockPurchase stockPurchas) {
		this.prodNo = stockOutProduct.getProdNo();
		if (stockPurchas == null) {
			this.exceptionInfo = "采购单号为[" + stockOutProduct.getPurNo() + "]的编号[" + this.prodNo + "]货品不存在,无法完成出库";
		} else {
			this.exceptionInfo = "采购单号为[" + stockOutProduct.getPurNo() + "]的编号[" + this.prodNo + "]货品对应数量不足,无法完成出库";

		}
	}

	public StockProductException(StockOutProduct stockOutProduct, ProductStock productStockFromDB) {
		this.prodNo = stockOutProduct.getProdNo();
		this.stockoutQuantity = stockOutProduct.getStockoutQuantity();
		if (productStockFromDB == null) {
			this.exceptionInfo = "编号[" + this.prodNo + "]货品不存在，或已移除,无法出库";
		} else if (stockoutQuantity < 1L) {
			this.exceptionInfo = "当前编号" + this.prodNo + "货品出库的数量小于1,无法出库";
		} else {
			long qualifiedQuantity = productStockFromDB.getQualifiedQuantity()
					- productStockFromDB.getPreOccupyQuantity() - stockoutQuantity;
			long defectiveQuantity = productStockFromDB.getDefectiveQuantity() - stockoutQuantity;
			if (qualifiedQuantity < 0L) {
				this.exceptionInfo = "当前编号" + this.prodNo + "货品的良品数不足,需要出库：" + stockoutQuantity + ", 仍差："
						+ (qualifiedQuantity * -1) + "，无法出库";
			} else if (defectiveQuantity < 0L) {
				this.exceptionInfo = "当前编号" + this.prodNo + "货品的次品数不足,需要出库：" + stockoutQuantity + ", 仍差："
						+ (defectiveQuantity * -1) + "，无法出库";
			} else {
				this.exceptionInfo = "当前编号" + this.prodNo + "货品发生未知异常,无法出库";
			}
		}
	}

	public StockProductException(StockOutProduct stockOutProduct, List<Object[]> stockOutPurInfoVos) {
		this.prodNo = stockOutProduct.getProdNo();
		this.stockoutQuantity = stockOutProduct.getStockoutQuantity();
		if (stockOutPurInfoVos == null) {
			this.exceptionInfo = "编号[" + this.prodNo + "]货品未导入条形码或关联采购单,无法出库";
		} else {
			this.exceptionInfo = "编号[" + this.prodNo + "]货品发生未知异常，无法出库";
		}
	}

	public StockProductException(StockInProductVo stockInProductVo, List<Object[]> purInfos) {
		this.prodNo = stockInProductVo.getProdNo();
		this.exceptionInfo = "编号[" + this.prodNo + "]货品缺失条形码或采购单，无法入库";
	}

	public StockProductException(ProductBarCode productBarCodeTemp, Integer stockInType) {
		this.prodNo = productBarCodeTemp.getProdNo();
		if (stockInType != null && stockInType.equals(SZSCMConstant.STOCKIN_TYPE_2)) {
			this.exceptionInfo = "编号[" + this.prodNo + "]的货品条码[" + productBarCodeTemp.getCodeNo()
					+ "]未包裹出库，请修改后重新导入条形码";
		} else {
			this.exceptionInfo = "编号[" + this.prodNo + "]的货品条码[" + productBarCodeTemp.getCodeNo() + "]已存在，请修改后重新导入条形码";
		}
	}

	public StockProductException(String zhDesc) {
		this.exceptionInfo = zhDesc;
	}

	public String getExceptionInfo() {
		return this.exceptionInfo;
	}
}
